package com.donica.wamstest.utils;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.donica.wamstest.entity.FilelistVO;
import com.donica.wamstest.entity.PlaylistVO;
import com.donica.wamstest.entity.ProgramfileVO;

public class PlaylistUtil {
	public static PlaylistVO parsePlaylist(String file) throws Exception {

		PlaylistVO playlistVO = new PlaylistVO();

		// 1.创建Reader对象
		SAXReader reader = new SAXReader();

		// 2.加载xml
		Document document = reader.read(new File(file));
		// 3.获取根节点
		Element rootElement = document.getRootElement();
		playlistVO.setType(rootElement.getName());

		Iterator iterator1 = rootElement.elementIterator();
		while (iterator1.hasNext()) {
			Element ele1 = (Element) iterator1.next();
			if (ele1.getName().equals("version")) {
				playlistVO.setVersion(ele1.getText());
			} else if (ele1.getName().equals("airways")) {
				
				ArrayList<FilelistVO> filelistVOs = new ArrayList<FilelistVO>();

				Iterator iterator2 = ele1.elementIterator();
				while (iterator2.hasNext()) {
					
					FilelistVO filelistVO = new FilelistVO();

					// 节目单
					Element ele2 = (Element) iterator2.next();
					String filelist = ele2.attributeValue("name");
					filelistVO.setName(filelist);

					ArrayList<ProgramfileVO> fileVOs = new ArrayList<ProgramfileVO>();
					// 遍历子节点，获取文件列表
					Iterator iterator3 = ele2.elementIterator();
					while (iterator3.hasNext()) {
						
						ProgramfileVO fileVO = new ProgramfileVO();
						// file
						Element ele3 = (Element) iterator3.next();
						String tempString;
						fileVO.setProgramname(ele3.elementText("programname"));
						fileVO.setProgramtype(ele3.elementText("programtype"));
						
						SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd"); 

						tempString = ele3.elementText("startdate");
						Timestamp startdate = new Timestamp(sdf.parse(tempString).getTime());
						fileVO.setStartdate(startdate);
						
						tempString = ele3.elementText("stopdate");
						Timestamp stopdate = new Timestamp(sdf.parse(tempString).getTime());
						fileVO.setStopdate(stopdate);
						
						fileVO.setPlayparam(ele3.elementText("playparam"));
						fileVO.setFilename(ele3.elementText("filename"));
						
						tempString = ele3.elementText("filesize");
						int filesize = Integer.valueOf(tempString);
						fileVO.setFilesize(filesize);
						
						fileVO.setIcon(ele3.elementText("icon"));
						fileVO.setDescribe(ele3.elementText("describe"));
						fileVO.setFilekey(ele3.elementText("filekey"));
						
						tempString = ele3.elementText("duration");
						int duration = Integer.valueOf(tempString);
						fileVO.setDuration(duration);
						
						fileVO.setKeys(ele3.elementText("Keys"));
						
						fileVOs.add(fileVO);
					}
					filelistVO.setFiles(fileVOs);
					filelistVOs.add(filelistVO);
				}
				
				playlistVO.setFileList(filelistVOs);
			}
		}
		return playlistVO;
	}
}
